#include "eventloop.h"
#include "channel.h"
#include "poller.h"
#include "epoll_poller.h"

#include <cassert>
#include <iostream>

EventLoop::EventLoop()
    : looping_(false),
      quit_(false),
      poller_(new EpollPoller())
{}

EventLoop::~EventLoop() {
    delete poller_;
}

void EventLoop::loop() {
    looping_ = true;
    quit_ = false;

    while (!quit_) {
        activeChannels_.clear();

        poller_->poll(10000, activeChannels_);

        for (Channel* channel : activeChannels_) {
            channel->handleEvent();
        }
    }

    looping_ = false;
}

void EventLoop::quit() {
    quit_ = true;
}

void EventLoop::updateChannel(Channel* channel) {
    assert(channel != nullptr);
    poller_->updateChannel(channel);
}

void EventLoop::removeChannel(Channel* channel) {
    assert(channel != nullptr);
    poller_->removeChannel(channel);
}
